<?php

namespace App\Task;

use App\Cache\RedisKey;
use App\Helpers\RedisUtil;
use App\Model\Setting\SettingAppHidden;
use App\Model\User;
use App\Model\UserProfile;
use App\Model\UserRecValue;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\DbConnection\Db;

/**
 * 渠道屏蔽的用户
 * @Crontab(name="ExcludeUsersByAppHiddenTask", rule="*\/30 * * * * *", callback="execute", memo="渠道屏蔽的用户", singleton=true)
 */
class ExcludeUsersByAppHiddenTask
{
    /**
     * @return void
     */
    public function execute()
    {
        log_info(__CLASS__ . "==Start==" . date('Y-m-d H:i:s'));

        $this->getExcludeUser();

        log_info(__CLASS__ . "==end==" . date('Y-m-d H:i:s'));
    }

    public function getExcludeUser()
    {
        log_info(__CLASS__ . "==0:Total：0");

        $excludeUser = [];
        $setting = SettingAppHidden::getSetting();
        foreach ($setting as $item => $value) {
            $unionWhere = [];
            $where = " 1 ";

            // 渠道
            if (!empty($value['channel'])) {
                $curChannel  = implode(',', $value['channel']);
                $unionWhere[] = " t_user.cur_channel in ({$curChannel})";
            }

            // 城市
            if (!empty($value['hidden_city']) && $value['hidden_un_know_city']) {
                $hiddenCity = "'" . join("','", array_values($value['hidden_city']) ) . "'";
                $unionWhere[] = " (t_user_profile.locate_city in ({$hiddenCity}) or t_user_profile.locate_city = '') ";
            } elseif (!empty($value['hidden_city']) && $value['hidden_un_know_city'] == 0) {
                $hiddenCity = "'" . join("','", array_values($value['hidden_city']) ) . "'";
                $unionWhere[] = " t_user_profile.locate_city in ({$hiddenCity}) ";
            } elseif (empty($value['hidden_city']) && $value['hidden_un_know_city']) {
                $unionWhere[] = " t_user_profile.locate_city = '' ";
            }

            // 版本
            if (!empty($value['version'])) {
                $unionWhere[] = " t_user.version = '{$value['version']}'";
            }

            $appHiddenRichValue = env('APP_HIDDEN_RICH_VALUE', 5000);
            $unionWhere[] = " t_user_profile.rich_value <  " . $appHiddenRichValue . ' ';

            $where .= ' AND (' . implode(' AND ', $unionWhere) . ')';

            $userIds = Db::table('user_profile')->join('user','user.id','=','user_profile.user_id')
                ->whereRaw($where)
                ->where(['user.status' => User::STATUS_OK])
                ->where('user_profile.updated_at', '>=', date('Y-m-d H:i:s', strtotime('-15 day')))
                ->where('gender', '=', 1)
                ->pluck('user_id')->toArray();

            $arrTotal = count($userIds);

            log_info(__CLASS__ . "=={$item}:Total：" . $arrTotal);

            if ($arrTotal) {
                $excludeUser = array_merge($excludeUser, $userIds);
            }
        }

        $excludeTotal = count($excludeUser);

        log_info(__CLASS__ . "==Total：" . $excludeTotal);

        $redis = RedisUtil::getRedis();

        $key = RedisKey::appHiddenUser();

        $redis->del($key);

        if ($excludeTotal) {
            $redis->sAddArray($key, $excludeUser);
        }
    }
}
